import Taro from '@tarojs/taro';
import { drakColors, lightColors } from '@/constants/Colors';
import { AppThemeMode } from './Contants';
// import { getData } from '../tool';

const themeModeKey = 'APP_THEME_MODE_KEY';
const themeModeFollowSystemKey = 'APP_THEME_MODE_FOLLOW_SYSTEM_KEY';

type TListener = (mode: AppThemeMode) => void;

function modeT(mode: string): AppThemeMode {
  return mode === 'dark' ? 'dark' : 'light';
}

class AppTheme {
  currentMode: AppThemeMode;

  appState: 'show' | 'hide';

  listenerList: TListener[];

  isFollowSystem: boolean;

  constructor() {
    const currentTheme = Taro.getSystemInfoSync().theme!;
    // this.currentMode = modeT(currentTheme);
    this.currentMode = 'light';
    this.listenerList = [];
    this.appState = 'show';
    this.isFollowSystem = true;
    Taro.onAppShow(() => {
      this.appState = 'show'
      this.handleAppStateChange('show')
    })
    Taro.onAppHide(() => {
      this.appState = 'hide'
      this.handleAppStateChange('hide')
    })
    // AppState.addEventListener('change', this.handleAppStateChange);
    // Appearance.addChangeListener(({ colorScheme }: any) => {
    //   console.log('Switched to', colorScheme, 'mode', this.isFollowSystem);
    //   if (this.appState === 'active') {
    //     if (this.isFollowSystem) {
    //       this.modeChange(colorScheme);
    //     }
    //   }
    // });
    wx.onThemeChange((res) => {
      if (this.appState === 'show') {
        if (this.isFollowSystem) {
          // console.log("theme => ",res.theme)
          this.modeChange(res.theme)
        }
      }
    })
  }

  init = async () => {
    // const storeageRes = await AsyncStorage.getItem(themeModeFollowSystemKey);
    const storeageRes = Taro.getStorageSync(themeModeFollowSystemKey);
    if (storeageRes) {
      this.isFollowSystem = storeageRes === '1';
      if (!this.isFollowSystem) {
        // const res = await AsyncStorage.getItem(themeModeKey);
        const res = await Taro.getStorageSync(themeModeKey);
        if (res) {
          this.currentMode = 'light';
          // this.currentMode = modeT(res);
        }
      }
    }
  }

  handleAppStateChange = (nextAppState: 'show' |'hide') => {
    if (nextAppState === 'show') {
      if (this.isFollowSystem) {
        const currentTheme = Taro.getSystemInfoSync().theme!;
        // console.log("currentTheme => ", currentTheme)
        this.modeChange(currentTheme);
      }
    }
    this.appState = nextAppState;
  }

  modeChange = (newMode: string) => {
    // console.log('modeChange', newMode);
    // this.currentMode = modeT(newMode);
    this.currentMode = 'light';
    if (this.listenerList && this.listenerList.length > 0) {
      this.listenerList.map((l) => {
        if (l) {
          l(this.currentMode);
        }
      });
    }
    // const nav = getData('navigation');
    // if (nav) {
    //   const setParamsAction = NavigationActions.setParams({
    //     params: { isDark: this.isDarkMode },
    //     key: 'appTheme',
    //   });
    //   nav.dispatch(setParamsAction);
    // }
  }


  addListener(l: TListener) {
    if (!this.listenerList.includes(l)) {
      this.listenerList.push(l);
    }
  }

  removeListener(l: TListener) {
    const index = this.listenerList.indexOf(l);
    if (index > -1) {
      this.listenerList.splice(index, 1);
    }
  }

  get isDarkMode() {
    return this.currentMode === 'dark';
  }

  get appTheme() {
    const theme = this.isDarkMode ? drakColors : lightColors;
    theme.appTheme = this.currentMode;
    return theme;
  }

  followSystem = (follow: boolean) => {
    this.isFollowSystem = follow;
    const currentTheme = Taro.getSystemInfoSync().theme!;
    if (this.isFollowSystem && this.currentMode !== currentTheme) {
      this.modeChange(modeT(currentTheme));
    }
    // AsyncStorage.setItem(themeModeFollowSystemKey, follow ? '1' : '0');
    Taro.setStorageSync(themeModeFollowSystemKey, follow ? '1' : '0');
  }

  selectMode = (mode: AppThemeMode) => {
    if (this.currentMode !== mode) {
      this.modeChange(mode);
      // AsyncStorage.setItem(themeModeKey, mode);
      Taro.setStorageSync(themeModeKey, mode);
    }
  }
}


const appThemeMode = new AppTheme();

export default appThemeMode;
